//
//  AppDelegate.swift
//  CZWeibo31
//
//  Created by apple on 16/8/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//        let result = HMStatusDAL.getCache()
//        print(result.count)
        setupAppStyle()
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // 设置背景
        self.window?.backgroundColor = UIColor.whiteColor()
        // 设置根视图
        setupRootVc()
        // 成为主窗口并显示
        self.window?.makeKeyAndVisible()
        // 接收通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootViewController:", name: WBSWITCHROOTVCNOTIFICATION, object: nil)
        return true
    }
    // 设置默认根控制器
    func setupRootVc(){
        // 登陆 -->HMWelcomeViewController
        if HMUserAccountViewModel.sharedTools.isLogin {
            self.window?.rootViewController = HMWelcomeViewController()
        }else {
            // 没有登陆 -->HMMainViewController
            self.window?.rootViewController = HMMainViewController()
        }
    }
    
    // 接收通知切换根控制器
    func switchRootViewController(noti: NSNotification){
        // 如果object 为nil 代表是welcomeVc 发送的通知 反之是oauthVc 发送的通知
        if noti.object == nil {
            // 欢迎界面
            self.window?.rootViewController = HMMainViewController()
        }else {
            // OAuthVc
            self.window?.rootViewController = HMWelcomeViewController()
        }
    }
    
    // 设置app
    func setupAppStyle(){
        // 设置全局颜色(tintColor 控件颜色) 必须在初始化项目之前设置 设置成功以后 全局有效
       // UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = AppThemeColor
    }
    
    // 析构函数
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // 请求微博数据缓存
        HMStatusDAL.deleteCache()
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

